package com.example.lanchat;

import android.os.Message;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class TcpClient {
    Thread socketThread, writeThread, readThread;
    String ip, msg;
    Socket socket = null;
    Boolean isRunning;
    Boolean isOpen = false;
    public static final int MSG_RECEIVE = 1;
    android.os.Handler myHandler;

    public TcpClient(android.os.Handler myHandler) {
        this.myHandler = myHandler;
    }

    //启动线程，创建socket
    public void startClient(String ip) throws IOException {
        this.ip = ip;
        socketThread = new Thread(new SocketThread());
        socketThread.start();
        isRunning = true;
    }
    public void sendMSG(String msg){
        this.msg=msg;
        isOpen=true;
    }
    class SocketThread implements Runnable {

        @Override
        public void run() {
            //指定服务器和端口
            try {
                socket = new Socket(ip, 6666);
            } catch (IOException e) {
                e.printStackTrace();
            }
            Message message = new Message();
            message.what=TcpServer.MSG_TCPCONN;
            myHandler.sendMessage(message);
            readThread = new Thread(new ReadThread());
            readThread.start();
            writeThread = new Thread(new WriteThread());
            writeThread.start();
            MainActivity.isBluetooth=false;
        }
    }

    class ReadThread implements Runnable {

        @Override
        public void run() {
            try {
                InputStream input = socket.getInputStream();
                OutputStream output = socket.getOutputStream();
                processMessage(input, output);
            } catch (IOException e) {
                e.printStackTrace();
                try {
                    socket.close();
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }
        }

        private void processMessage(InputStream input, OutputStream output) throws IOException {
            BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8));
            while (isRunning) {
                //读取对方发送的消息，并更新
                String ss = reader.readLine();
                if (ss == null) {

                } else {
                    Message message = new Message();
                    message.what = MSG_RECEIVE;
                    message.obj = ss;
                    myHandler.sendMessage(message);
                }
            }
        }
    }

    class WriteThread implements Runnable {

        @Override
        public void run() {
            try {
                InputStream input = socket.getInputStream();
                OutputStream output = socket.getOutputStream();
                processMessage(input, output);
                MainActivity.isBluetooth=false;
            } catch (IOException e) {
                e.printStackTrace();
                try {
                    socket.close();
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }
        }

        private void processMessage(InputStream input, OutputStream output) throws IOException {
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output, StandardCharsets.UTF_8));
            //发送消息调用，按下按钮发送
            while (isRunning) {
                if (isOpen) {
                    writer.write(msg + "\n");
                    writer.flush();
                    isOpen = false;
                }
            }
        }
    }
}


